package escom.dds.swing.graficas;

import java.awt.Color;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.List;

import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

import escom.dds.modelo.Alumno;
import escom.dds.modelo.Calificacion;
import escom.dds.rmi.dataset.IDataSet;

public class Promedios extends ApplicationFrame {

	private static final long serialVersionUID = 1L;

	public Promedios(String title) {
		super(title);
		JPanel chartPanel = createDemoPanel();
		chartPanel.setPreferredSize(new java.awt.Dimension(1000, 540));
		setContentPane(chartPanel);
	}

	/**
	 * Creates a sample dataset.
	 * 
	 * @return A sample dataset.
	 */
	private static CategoryDataset createDataset() {
		try {
			DefaultCategoryDataset dataset = new DefaultCategoryDataset();
			IDataSet datos = (IDataSet)Naming.lookup("//localhost/dataset");
			List<Alumno> alumnos = datos.getAlumnos();
			
			for(Alumno alumno : alumnos)
			{
				dataset.setValue(promedioA(alumno.getCalificaciones()), alumno.getCarrera().getNombre(), alumno.getNombre()+alumno.getApPaterno());
			}
			return dataset;
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return new DefaultCategoryDataset();		
	}
	
	public static float promedioA(List<Calificacion> calif)
	{
		int i=0;
		float total=0;
		for(Calificacion c : calif)
		{
			total+=c.getPuntaje();
			i++;
		}
		return total / i;
	}

	/**
	 * Creates a chart.
	 * 
	 * @param dataset
	 *            the dataset.
	 * 
	 * @return A chart.
	 */
	private static JFreeChart createChart(final CategoryDataset dataset, String title, String x, String y) {
		// create the chart...
		JFreeChart chart = ChartFactory.createBarChart(
				title, // chart title
				x, // domain axis label
				y, // range axis label
				dataset, // data
				PlotOrientation.HORIZONTAL, // orientation
				false, // include legend
				true, false);
		chart.setBackgroundPaint(Color.white);
		CategoryPlot plot = (CategoryPlot) chart.getPlot();
		plot.setBackgroundPaint(Color.LIGHT_GRAY);
		plot.setRangeGridlinePaint(Color.white);
		plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
		BarRenderer renderer = (BarRenderer) plot.getRenderer();
		renderer.setBaseItemLabelsVisible(true);
		renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		CategoryAxis categoryAxis = plot.getDomainAxis();
		categoryAxis.setCategoryMargin(0.0);
		categoryAxis.setUpperMargin(0.02);
		categoryAxis.setLowerMargin(0.02);
		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		rangeAxis.setUpperMargin(0.10);

		return chart;
	}

	/**
	 * Creates a panel for the demo (used by SuperDemo.java).
	 * 
	 * @return A panel.
	 */
	public static JPanel createDemoPanel() {
		JFreeChart chart = createChart(createDataset(), "Calificaciones por Alumno", "Alumno", "Calificación");
		return new ChartPanel(chart);
	}

	/**
	 * Starting point for the demonstration application.
	 * 
	 * @param args
	 *            ignored.
	 */
	public static void main(String[] args) {

		Promedios demo = new Promedios("Gestión de Alumnos");
		demo.pack();
		RefineryUtilities.centerFrameOnScreen(demo);
		demo.setVisible(true);

	}
}
